﻿
(function ($) {
  jQuery.fn.scrollbar = function () {

    this
      .filter(function () {
        var $this = $(this);
//        console.log('innerHeight: ' + $this.innerHeight());
//        console.log('height: ' + parseInt($this.css('height')));
//        console.log('max-height: ' + parseInt($this.css('max-height')));
//        console.log('clientHeight: ' + $this[0].clientHeight);
//        console.log('scrollHeight: ' + $this[0].scrollHeight);
        return ($this.innerHeight() == parseInt($this.css('height'))) && ($this[0].clientHeight < $this[0].scrollHeight);
      })
      .each(function () {

        var scrolling = false;

        $(this)
          //.css('overflow-y', 'hidden')
          .scroll(function () {
            if (!scrolling) {
              var $this = $(this);
              var $handle = $this.prev().find('.handle');
              var $container = $handle.parent();
              var p = $this.scrollTop() / ($this[0].scrollHeight - $this.outerHeight());
              $handle.css('top', Math.min($container.height() * p, $container.height() - $handle.outerHeight(true)));
            }
          })
          .wrap('<div class="scroll-container"/>')
          .parent()
            .prepend('<div class="handle-container"><span class="handle"></span></div>')
            .find('.handle-container').click(function (e) {
              var $handle = $('.handle', this);
              var $container = $(this);
              var $scroll = $container.next();
              var layerY = e.pageY - $container.offset().top;
              var p = layerY / ($container.height() - $handle.outerHeight(true));
              $scroll.scrollTop(($scroll[0].scrollHeight - $scroll.outerHeight()) * p);
            })
            .find('.handle').draggable({
              containment: 'parent',
              axis: 'y',
              start: function () {
                scrolling = true;
              },
              stop: function () {
                scrolling = false;
              },
              drag: function (event, ui) {
                var $handle = ui.helper;
                var $container = $handle.parent();
                var $scroll = $container.next();
                var p = ui.position.top / ($container.height() - $handle.outerHeight(true));
                $scroll.scrollTop(($scroll[0].scrollHeight - $scroll.outerHeight()) * p);
              }
            })
      });

    return this;

  };
})(jQuery);


(function ($) {
  jQuery.fn.zoom = function () {

    function setLens(e, $lens, $container) {
      var offset = $container.offset();
      var containerWidth = $container.width();
      var containerHeight = $container.height();
      var lensHalfWidth = $lens.outerWidth(true) / 2;
      var lensHalfHeight = $lens.outerHeight(true) / 2;
      var x = Math.max(0, Math.min(e.pageX - offset.left, containerWidth - lensHalfWidth) - lensHalfWidth);
      var y = Math.max(0, Math.min(e.pageY - offset.top, containerHeight - lensHalfHeight) - lensHalfHeight);
      $lens.css({ left: x, top: y });
      var $zoomer = $lens.next();
      var $zoomImg = $zoomer.children('img');
      var zoomRatio = $lens.data('zoom-ratio');
      $zoomImg.css({ 'left': -(x * 1 / zoomRatio[0]), 'top': -(y * 1 / zoomRatio[1]) });
    }

    $(this).parent().eq(0).delegate('.zoom.zoom-on.zoom-ready', 'mousemove', function (e) {
      var $container = $(this);
      var $lens = $container.children('.lens');
      setLens(e, $lens, $container);
    });

    this
      .each(function () {

        $(this)
          .append('<div class="lens"/>')
          .append('<div class="zoomer"/>')
          .mouseleave(function (e) {
            $(this).removeClass('zoom-on');
          })
          .click(function (e) {

            e.preventDefault();

            var $container = $(this);
            $container.toggleClass('zoom-on');
            var $lens = $container.children('.lens');
            var $zoomer = $container.children('.zoomer');

            if ($container.hasClass('zoom-on')) {
              if (!$zoomer.children('img').length) {

                $('<img/>')
                  .load(function () {
                    $lens.parent().addClass('zoom-ready');
                    var zoomRatio = [$zoomer.width() / $(this).width(), $zoomer.height() / $(this).height()];
                    $lens.data('zoom-ratio', zoomRatio).css({ width: $container.width() * zoomRatio[0], height: $container.height() * zoomRatio[1] });
                    setLens(e, $lens, $container);
                  })
                .attr('src', $container.attr('href'))
                .appendTo($zoomer);

              } else {
                setLens(e, $lens, $container);
              }
            }

            return false;

          })
      });

    return this;

  };
})(jQuery);



(function ($) {
  jQuery.fn.carousel = function () {
    return this.each(function () {

      var $this = $(this);
      var $items = $this.find('.items > div');
      var $pageInfo = $this.children('.page-info');
      var index = 0;
      var length = $items.length;
      function setIndex(i) {
        index = i;
        $items.hide().eq(index).show();
        $pageInfo.text((index + 1) + '/' + length);
      }
      $this
        .delegate('.prev', 'click', function (e) {
          e.preventDefault();
          setIndex(index == 0 ? (length - 1) : (index - 1));
          return false;
        })
        .delegate('.next', 'click', function (e) {
          e.preventDefault();
          setIndex(index == (length - 1) ? 0 : (index + 1));
          return false;
        });

    });
  }
})(jQuery);



(function ($) {
  jQuery.fn.styleCurrentLinks = function () {
    return this.each(function () {
      $('a[href=\'' + window.location.pathname + window.location.search + '\']', this).addClass('current');
    });
  }
})(jQuery);



(function ($) {
  jQuery.fn.productImage = function () {
    return this.each(function () {

      $(this).bind('error', function (e) {
        // update image src
        var $this = $(this);
        var src = $this.attr('src');
        var index1 = src.lastIndexOf('/') + 1;
        var index2 = src.lastIndexOf('_');
        $this.attr('src', src.substr(0, index1) + 'Error' + src.substr(index2));
      });

    });
  }
})(jQuery);



(function ($) {
  jQuery.fn.panels = function (accordion, scrollbar) {
    return this.each(function () {

      if (scrollbar) {
        $(this).find(scrollbar === true ? '.scroll' : scrollbar).scrollbar();
      }

      $(this)
        .delegate('div > h4', 'click', function (e) {
          var $this = $(this);
          var $parent = $this.parent();
          var $panel = $this.next();
          if (accordion) {
            $parent.siblings(':not(.closed)').addClass('closed').children('.panel').slideUp();
          }
          $panel.slideToggle();
          $parent.toggleClass('closed');
        })
        .children('div[data-closed=true]').addClass('closed');

    });
  }
})(jQuery);




(function ($) {
  jQuery.fn.addressEditor = function () {
    
    function showHide($countries, $lookup) {
      if ($countries.val() == 'GBR') {
        $lookup.show();
      } else {
        $lookup.hide();
      }
    }

    return this.each(function () {
      
      var $this = $(this);
      var $countries = $this.find('.address-country select');
      var $lookup = $this.find('.address-lookup');
      var $addressList = $this.find('.address-list');
      
      showHide($countries, $lookup);

      $countries.change(function (e) {
        showHide($(this), $lookup);
      });

      $lookup.find('.address-searchbutton').click(function(e) {
        qtvpca.findAddress(
          $(this).attr('id'), 
          $this.find('.building-searchbox input').attr('id'), 
          $this.find('.postcode-searchbox').attr('id'), 
          $addressList.attr('id'), 
          $this.find('.address-line1 input').attr('id'), 
          $this.find('.address-line2 input').attr('id'), 
          $this.find('.address-line3 input').attr('id'), 
          $this.find('.address-town input').attr('id'), 
          $this.find('.address-county input').attr('id'), 
          $this.find('.address-postcode input').attr('id'), 
          $countries.attr('id'));
      });

      $addressList.change(function(e) {
        qtvpca.getSingleAddress(
          $(this).attr('id'), 
          $this.find('.building-searchbox input').attr('id'), 
          $this.find('.postcode-searchbox').attr('id'), 
          $addressList.attr('id'), 
          $this.find('.address-line1 input').attr('id'), 
          $this.find('.address-line2 input').attr('id'), 
          $this.find('.address-line3 input').attr('id'), 
          $this.find('.address-town input').attr('id'), 
          $this.find('.address-county input').attr('id'), 
          $this.find('.address-postcode input').attr('id'), 
          $countries.attr('id'));
      });

    });
  }
})(jQuery);



(function ($) {
  jQuery.fn.horizScroll = function (config) {
    
    config = config || { leftSelector: '.browse-left', containerSelector: '.container', rightSelector: '.browse-right' };
    
    return this
      .filter(function () {
        var $ctnr = $(this).find(config.containerSelector);
        return $ctnr[0].clientWidth < $ctnr[0].scrollWidth;
      })
      .each(function () {

        var $this = $(this);
        $ctnr = $this.find(config.containerSelector);

        $this
          .delegate(config.leftSelector, 'click', function (e) {
            e.preventDefault();
            $ctnr.scrollLeft(Math.max(0, $ctnr.scrollLeft() - $ctnr.innerWidth()));
            return false;
          })
          .delegate(config.rightSelector, 'click', function (e) {
            e.preventDefault();
            $ctnr.scrollLeft(Math.min($ctnr[0].scrollWidth - $ctnr.innerWidth(), $ctnr.scrollLeft() + $ctnr.innerWidth()));
            return false;
          })
          .find(config.leftSelector + ',' + config.rightSelector).css('visibility', 'visible');

    });
  }
})(jQuery);





(function ($) {
  jQuery.fn.modalDialog = function (config) {
    config = config || {};
    return this.each(function () {
      var $this = $(this);
      if ($this.data('dialog')) {
        $this.dialog('open');
      } else {
        $this.dialog({ position: ['center', 100], hide: 'fade', closeText: 'Close', modal: true, draggable: config.draggable || false, resizable: config.resizable || false, width: config.width || 700, height: config.height || 'auto', dialogClass: config.dialogClass || '' });
        var $title = $this.closest('.ui-dialog').find('.ui-dialog-titlebar')
        if (config.hideCloseButton) {
          $title.hide();
        } else {
          $title.html('<div class="close"><a href="#">Close</a></div>');
        }
      }
    });
  }
})(jQuery);